﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;

namespace Jy123.Web.Gift
{
    public partial class Index : System.Web.UI.Page
    {
        private int userID ;//暂用用户ID，写死，到时候用COOKIE替换过来
        List<int?> count;
        string strAlert = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

           

            string strSGift = Request.Form["sendGift"];
            string strFrdID = Request.Form["selFrd"];
            if (!string.IsNullOrEmpty(strSGift))
            {
                if (strSGift == "送出礼物")
                {
                    if (!string.IsNullOrEmpty(strFrdID))
                    {
                        Jy123.Model.Gift_Record mGiftRecord = new Jy123.Model.Gift_Record();
                        Jy123.BLL.Gift_Record bllGiftRecord = new Jy123.BLL.Gift_Record();

                        Jy123.Model.User_Main mUser = new Jy123.Model.User_Main();
                        mUser = new Jy123.BLL.User_Main().GetModel(userID);

                        mGiftRecord.FUser = userID;//int.Parse(Request.Form["selFrd"]);
                        mGiftRecord.FUserName = mUser.UserName;
                        string strHidGift = Request.Form["hidgift"];
                        if (!string.IsNullOrEmpty(strHidGift)) { mGiftRecord.FGift = Convert.ToInt32(Request.Form["hidgift"]); }
                        else { strAlert = "<script>$(function(){$.moonbox.ok('请选择礼物!');})</script>"; }

                        mGiftRecord.FReciever = int.Parse(strFrdID);

                        mGiftRecord.FRecieverName = new Jy123.BLL.User_Main().GetModel(int.Parse(Request.Form["selFrd"])).UserName;
                        mGiftRecord.GMessage = Request.Form["txtMsg"].ToString();
                        mGiftRecord.CreatTime = DateTime.Now;
                        bllGiftRecord.Add(mGiftRecord);
                        //加入动态
                        Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                        Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                        mUserForState = bllUserForState.GetModel(userID);
                        Jy123.Model.User_Main mUserFrdForState = new Jy123.Model.User_Main();
                        mUserFrdForState = bllUserForState.GetModel(int.Parse(strFrdID));
                        Jy123.Model.Public_Gift mGift = new Jy123.Model.Public_Gift();
                        Jy123.BLL.Public_Gift bllGift = new Jy123.BLL.Public_Gift();
                        mGift = bllGift.GetModel(Convert.ToInt32(mGiftRecord.FGift));

                        new Jy123.BLL.Public.State().CreateSendGiftState(mUserForState,mUserFrdForState,mGift);
                        //加入动态
                    }
                    else
                    { strAlert = "<script>$(function(){$.moonbox.error('请先添加要赠送的好友!');})</script>"; }
                }
            }
            #region 公共部分
            StringBuilder strHtml = new StringBuilder(Cache["user_gift"].ToString());//取得主HTML页面
            strHtml = strHtml.Replace("$Header$", Cache["user_header2"].ToString());
            strHtml = strHtml.Replace("$Footer$", Cache["user_footer"].ToString());
            strHtml = strHtml.Replace("$LeftUserCmd$", Cache["user_navigation_down"].ToString());
            strHtml = strHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());
            //统计站内信数量
            Jy123.BLL.User.Letter bllLetter = new Jy123.BLL.User.Letter();
            int newLetterCount = bllLetter.CountNewLetter(userID);
            strHtml = strHtml.Replace("$NewMsg$", newLetterCount.ToString());
            #endregion            
            strHtml = strHtml.Replace("$FrdSelectOption$", FriendList());//好友下拉表            
            strHtml = strHtml.Replace("$GiftClass_a$",GiftClassCount());//礼物分类类型                     
            strHtml = strHtml.Replace("$GiftListli$", GiftPhotoList());//礼物图片列表   

            Response.Write(strHtml.ToString()+strAlert);
        }

        //好友下拉表
        private string FriendList()
        {
            StringBuilder sbFrdSelect = new StringBuilder();
            string strFrdSelect = Cache["user_gift_friendselectoption"].ToString();
            DataTable dtFrdList = new Jy123.BLL.User.Friend().GetFriendList(1, int.MaxValue, userID, out count).Tables[0];
            for (int i = 0; i < dtFrdList.Rows.Count; i++)
            {
                string str = strFrdSelect;
                str = str.Replace("$FFriend$", dtFrdList.Rows[i]["FFriend"].ToString())
                    .Replace("$UserName$", dtFrdList.Rows[i]["UserName"].ToString());
                sbFrdSelect.Append(str);
            }
            return sbFrdSelect.ToString();
        }
        //礼物分类类型
        private string GiftClassCount()
        {           
            StringBuilder sbGiftClass = new StringBuilder();
            string strGiftClass = Cache["user_giftclass_a"].ToString();
            DataTable dtGiftClass = new BLL.User.Gift().GiftClassCount().Tables[0];
            for (int i = 0; i < dtGiftClass.Rows.Count; i++)
            {
                string str = strGiftClass;
                str = str.Replace("$GiftClassId$", (dtGiftClass.Rows[i]["ID"].ToString()==string.Empty)?"0":dtGiftClass.Rows[i]["ID"].ToString())
                    .Replace("$GiftClassName$", (dtGiftClass.Rows[i]["GiftClassName"].ToString() == string.Empty) ? "未分类" : dtGiftClass.Rows[i]["GiftClassName"].ToString())
                    .Replace("$GiftCount$", dtGiftClass.Rows[i]["GiftCount"].ToString());
                sbGiftClass.Append(str);
            }
            return sbGiftClass.ToString();
        }
        //礼物图片列表
        private string GiftPhotoList()
        {
            StringBuilder sbGiftImg = new StringBuilder();
            string strGiftImg = Cache["user_giftphoto_li"].ToString();
            DataTable dtGiftImg = new Jy123.BLL.User.Gift().GiftList(1, 24, string.Empty, out count).Tables[0];
            for (int i = 0; i < dtGiftImg.Rows.Count; i++)
            {
                string str = strGiftImg;
                str = str.Replace("$Picture$", dtGiftImg.Rows[i]["Picture"].ToString())
                    .Replace("$GiftID$", dtGiftImg.Rows[i]["ID"].ToString())
                    .Replace("$GName$", dtGiftImg.Rows[i]["GName"].ToString());
                sbGiftImg.Append(str);
            }
            string pageHtml = @"
                            <li class=""page"">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b> 
                            </li>";
            if (count[1] > 1)
            {
                MoonPage mp = new MoonPage(true, false);
                mp.Count = Convert.ToInt32(count[1]);
                sbGiftImg.AppendFormat(pageHtml, mp.GetStrPage(1, "no", "sel", "fillhtml('giftitems','/Gift/GiftItems.ashx?page=$num$')"),
                    1, 24, count[0], "");
            }
            sbGiftImg.Append(@"<script language=""javascript"" type=""text/javascript"">$(function(){bindGiftItems();})</script>");
            return sbGiftImg.ToString();
        }
    }
}
